using System;
using System.Collections.Generic;
using Firefly.Framework;
using Firefly.Framework.Components;
using Firefly.Framework.Graphics;
using Firefly.Framework.Input;
using Firefly.Framework.Windows;
using Firefly.Framework.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using WindowsExample.Windows;

namespace WindowsExample
{
    public class WindowsTest : Engine
    {
        private LoginDialog _loginDialog;
        private DebugInformation _debugDialog;
        private FrameRateCounter _frameRate;
        private TextCompositor _engineKey;

        public WindowsTest()
        {
            ScreenWidth = 1024;
            ScreenHeight = 800;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            _debugDialog = new DebugInformation();
            _loginDialog = new LoginDialog(this);
            _loginDialog.Focus();
            WindowsService.Instance.Enabled = true;
            WindowsService.Instance.Visible = true; 
            _engineKey = new TextCompositor("Fonts\\Kootenay") { Position = new Vector2(5f, 785f), Color = Color.GreenYellow };
            _engineKey.LoadContent();
            _engineKey.Text = "[Engine] Exit : Esc";
            _frameRate = new FrameRateCounter(this, "Fonts\\Kootenay") { Position = new Vector2(5f, 5f), Color = Color.GreenYellow };
            _frameRate.LoadContent();
        }

        protected override void UnloadContent()
        {
            _frameRate.Dispose();
            _engineKey.Dispose();
        }

        protected override void Update(GameTime gameTime)
        {
            _frameRate.Update(gameTime);
            _debugDialog.FramesPerSecond = string.Format("FPS: {0}", _frameRate.FrameRate);
            _debugDialog.UiElementCount = string.Format("UI Element Count: {0}", UiElementCount(WindowsService.Instance.Desktop.Children));
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsService.Instance.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
            GraphicsService.Instance.FillMode = FillMode.Solid;
            _frameRate.Draw(gameTime);
            _engineKey.Draw(gameTime);
        }

        private int UiElementCount(List<Control> children)
        {
            int count = 0;

            if (children != null)
            {
                count = children.Count;
            }

            foreach (Control c in children)
            {
                count += UiElementCount(c.Children);
            }

            return count;
        }

        #region IInputProcessor Members

        public override void InitializeInputMaps()
        {
            HoldThreshold = 100;
            KeyMaps = new List<KeyMap>();
            KeyMaps.Add(new KeyMap(new Keys[] { Keys.Escape }));
            KeyMaps[0].MapPressed += new EventHandler<KeyMapEventArgs>(OnExit);            
        }

        private void OnExit(object sender, KeyMapEventArgs e)
        {
            Exit();
        }

        #endregion
    }
}
